﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Entity.Models;
using JinfuyuanProject.Entity.ViewModels;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JinfuyuanProject.Controllers
{
    public class ParturientController : Controller
    {
        private readonly IMParturientService _service;
        private readonly ParturientFilter _filter;

        public ParturientController(IMParturientService service, ParturientFilter filter)
        {
            _service = service;
            _filter = filter;
        }
        // GET: Parturient
        #region 页面
        public ActionResult Index()
        {

            return View();
        }
        public ActionResult Add()
        {
            string name = Request.QueryString["name"];
            if (string.IsNullOrEmpty(name))
            {
                return View();
            }

            ViewBag.ParturientName = name;
            return View();
        }
        public ActionResult Revisit()
        {
            string name = Request.QueryString["name"];
            ViewBag.ParturientName = name;
            MParturient parturient = _service.GetModel(name);
            return View(parturient);
        }
        #endregion
        #region 接口
        public JsonResult Insert(string data)
        {
            MParturient parturient = Newtonsoft.Json.JsonConvert.DeserializeObject<MParturient>(data);
            Hashtable ht = new Hashtable();
            parturient.IsDelete = 0;
            if(parturient.EntryTime == null)
            {
                parturient.EntryTime = DateTime.Now;
            }
            Staff staff = (Staff)Session["user"];
            if(staff!=null&&staff.RoleId == 2)
            {
                parturient.Salesman = staff.Name;
                parturient.SalesmanNumbers = staff.Numbers;
            }
            parturient.CreateTime = parturient.EntryTime;
            var result = _service.Insert(parturient);
            ht["code"] = 0;
            ht["msg"] = "添加成功";
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Update(string data)
        {
            string name = Request.QueryString["name"];
            MParturient parturient = Newtonsoft.Json.JsonConvert.DeserializeObject<MParturient>(data);
            Hashtable ht = new Hashtable();
            if (name != parturient.ParturientName)
            {
                Delete(name);
                Insert(data);
            }
            var result = _service.Update(parturient);
            ht["code"] = 0;
            ht["msg"] = "修改成功";
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Delete(string name)
        {
            Hashtable ht = new Hashtable();
            MParturient parturient = _service.GetModel(name);
            if (parturient == null)
            {
                ht["code"] = 1;
                ht["msg"] = "数据不存在";
                return Json(ht, JsonRequestBehavior.AllowGet);
            }
            var result = _service.Delete(parturient);
            ht["code"] = 0;
            ht["msg"] = "删除成功";
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        public JsonResult DataList(int page, int limit, string name, string phone, string address)
        {

            _filter.page = page;
            _filter.limit = limit;
            _filter.ParturientName = name;
            _filter.ParturientPhone = phone;
            _filter.Address = address;
            Hashtable ht = _service.Query(_filter);
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        public JsonResult userget()
        {
            string name = Request.QueryString["name"];
            if (string.IsNullOrEmpty(name))
            {
                return Json(new { code = 1, msg = "数据不存在" }, JsonRequestBehavior.AllowGet);
            }
            Hashtable ht = new Hashtable();
            MParturient parturient = _service.GetModel(name);
            if (parturient == null)
            {
                ht["code"] = 1;
                ht["msg"] = "数据不存在";
                return Json(ht, JsonRequestBehavior.AllowGet);
            }
            var result = _service.GetModel(name);
            ht["code"] = 0;
            ht["msg"] = "查询成功";
            ht["data"] = result;
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Back(string data)
        {
            Hashtable ht = new Hashtable();
            Revisit revisit = Newtonsoft.Json.JsonConvert.DeserializeObject<Revisit>(data);
            revisit.RevisitTime = DateTime.Now;
            string name = Request.QueryString["name"];
            MParturient parturient = _service.GetModel(name);
            parturient.EntryTime = DateTime.Now;
            if (parturient.Revisits == null) parturient.Revisits = new List<Revisit>();
            parturient.Revisits.Add(revisit);
            if (revisit == null)
            {
                ht["code"] = 1;
                ht["msg"] = "数据不存在";
                return Json(ht, JsonRequestBehavior.AllowGet);
            }
            var result = _service.Update(parturient);
            ht["code"] = 0;
            ht["msg"] = "查询成功";
            return Json(ht, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}